// ===============================================================================
// Microsoft Biztalk 2006 Rule Deployement Tool
//
// App.cs
//
// This file contains the implementation for the rule deployment tool.
//
// ===============================================================================
// Release history
// VERSION	DESCRIPTION
//   1.0	Initial release
//
// ===============================================================================
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft BizTalk
// Server 2006 release and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
// ==============================================================================

// The Rule Engine Deployment Wizard, which ships with Microsoft BizTalk 2006, 
// provides a graphical user interface to easily import and export policies and
// vocabularies However, this wizard is not scriptable and cannot be used to easily
// automate the deployment of policies. This Rule Deployment Tool sample is a 
// console application which uses command-line arguments to import and export 
// policies. As it is a command line application, you can use it to deploy policies 
// in a scriptable and automatable manner. 


using System;
using System.IO;
using System.Collections;
using System.Reflection;
using Microsoft.RuleEngine;
using Microsoft.BizTalk.RuleEngineExtensions;

namespace DeployRules
{
	partial class App
	{
		#region fields 
		// Array list containing the names of the files (import)
		private static ArrayList files = new ArrayList();
		// Array list containing the names of the policies
		private static ArrayList policies = new ArrayList();
		// Array list containing the names of the vocabularies
		private static ArrayList vocabularies = new ArrayList();

		// ParseCommandLine method assigns appropriate values to the following variables 
		// based on the command-line arguments passed to this program. 

		// To import or not to import
		private static bool importFlag = false;
		// To publish vocabulary or not to publish
		private static bool publishVocabulary = false;
		// To publish policy or not to publish
		private static bool publishPolicy = false;
		// To deploy policy while importing or not
		private static bool importDeployPolicy = false;
		// To export or not to export
		private static bool exportFlag = false;
		// To export all vocabularies or not
		private static bool exportAllVocabularies = false;
		// To export all policies or not
		private static bool exportAllPolicies = false;
		// To undeploy policy or not 
		private static bool undeployPolicies = false;
		// To deploy policy or not
		private static bool deployPolicies = false;
	    private static bool isSingleDeployment = false; //to deploy a single policy

        //keep track of inputs for Policy or Vocabulary
	    private static bool isPolicyImport = false;
	    private static bool isVocabularyImport = false;

		// To remove policy (vocabulary) or not
		private static bool removeFlag = false;
		// To show help or not
		private static bool help = false;

        private static bool exportToAPath = false;

		private static StreamWriter logWriter = null;
		private static string policyName = null;
		private static int policyMajorVer = 1;
		private static int policyMinorVer = 0;
		private static string vocabularyName = null;
		private static int vocabularyMajorVer = 1;
		private static int vocabularyMinorVer = 0;
		#endregion //fields

		/// <summary>
		/// Main method invokes the ParseCommandLine method, which parses the command-line arguments, 
		/// and updates the preceding flags and variables. Then, the main method does the following:
		/// If help flag is set - Displays the help for using the utility
		/// If importFlag is set - Inovokes Import method to import policies/vocabularies in to the rule engine database
		/// If exportFlag is set - Inovokes Export method to exports policies/vocabularies to a BRL XML file
		/// </summary>
		/// <param name="args">Refer to the descriptions for the arguments for the ParseCommandLine method
		/// </param>
		/// <returns>0 for success, -1 for failure</returns>
		[STAThread]
		static int Main(string[] args)
		{
			int result = 0;

			try
			{
				//Write a blank line
				Console.WriteLine();

				//Write the title along with the verson number
				// Displays "BizTalk 2004 Rule Deployment Tool v1.0"
				Console.WriteLine(GetTitle());

				//Write another blank line
				Console.WriteLine();

				// Parses the command line arguments and sets the
				// publishVocabulary, publishPolicy, importDeployPolicy, removePriorPolicy and help flags. 
				// It also adds the names of the files to import to files array
				ParseCommandLine(args);

				// if help flag is set, show the help
				if (App.help)
				{
					ShowHelp();
					return result;
				}

				if (App.importFlag)
				{
					// If importFlag is set, invoke Import method to import vocabularies or/and 
					// policies from BRL files to database
					result = Import();
				}
				else if (App.exportFlag == true)
				{
					// If exportFlag is set, invoke Export method to export vocabularies and 
					//policies from database to BRL files 
					result = Export();
				}
				else if (App.undeployPolicies == true)
				{
					result = UndeployPolicy();
				}
				else if (App.deployPolicies == true)
				{
					result = DeployPolicy();
				}
				else if (App.removeFlag == true)
				{
					if (policies.Count > 0)
					{		
						UndeployPolicy();//undeploy them all first
					    RemovePolicies();//remove them all
					}

					if (vocabularies.Count > 0)
					{
						//Now, remove vocabularies
						RemoveVocabulary();
					}

				}

				if (result > 0)
					LogMessage("Processing complete\r\n");
			}
			catch (Exception e)
			{
				//Log the message to console, optionally to a log file
				LogMessage(e.Message);
				result = -1;
			}
			finally
			{
				//If the StreamWriter object is opened, close it.
				if (logWriter != null)
					logWriter.Close();
			}

			// So that console window does not disapper when you debug from VS
			//Console.WriteLine("Press any key to continue");
			//Console.Read();

			return result;
		}
	}
}
